<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.6.1.js"></script>
</head>
<body>
<form id="products">
    <table border="1">
        <tr>
            <th>商品id</th><th>商品名称</th><th>提交</th>
        </tr>
        <tr>
            <td><input type="text" name="proId" value="1" id="proId"></td>
            <td><input type="text" name="proName" value="三文鱼" id="proName"></td>
            <td><input type="button" value="提交单个商品" onclick="submitProduct()"/></td>
        </tr>
        <tr>
            <td><input type="text" name="proId" value="2" id="proId2"></td>
            <td><input type="text" name="proName" value="红牛" id="proName2"></td>
            <td><input type="button" value="提交多个商品" onclick="submitProducts()"/></td>
        </tr>
    </table>
</form>
<script type="text/javascript">
    function submitProduct(){
        var proId = $("#proId").val();
        var proName = $("#proName").val();
        $.ajax({
            url: "${pageContext.request.contextPath}/getProduct",        //路径
            type: "post",                       //请求方式
            data: JSON.stringify({proId:proId,proName:proName}),     //传输数据
            contentType: "application/json;charset=UTF-8",           //请求头信息
            dataType: "json",       //预期服务器返回的数据类型
            success: function (response) {alert(response);}       //请求成功调用的函数
        });
    }
    function submitProducts(){
        var pro1 = {proId:$("proId").val(),proName:$("proName").val()};
        var pro2 = {proId2:$("proId2").val(),proName2:$("proName2").val()};
        $.ajax({
            url: "${pageContext.request.contextPath}/getProduct",        //路径
            type: "post",                       //请求方式
            data: JSON.stringify([pro1,pro2]),     //传输数据
            contentType: "application/json;charset=UTF-8",           //请求头信息
            dataType: "json",       //预期服务器返回的数据类型
            success: function (response) {alert(response);}       //请求成功调用的函数
        });
    }
</script>
</body>
</html>
